package com.dhsc.timer;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dhsc.constant.FtpFilePthContant;
import com.dhsc.domain.jkzx.qsjk.TQsjkrwLsVo;
import com.dhsc.domain.jkzx.qsjk.TQsjkrwVo;
import com.dhsc.ftp.FTPUtil;
import com.dhsc.local.dao.common.ICommonDao;
import com.dhsc.local.dao.qsjk.ITQsjkDao;
import com.dhsc.service.qsjk.ITQsjkService;
import com.dhsc.util.CommonUtil;
import com.dhsc.util.QsjkFilesUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName:  TQsjkrwTimer
 * @Description: 清算监控定时任务
 * @date:   2024年3月12日 下午1:56:57       
 * @author: clong
 * @Copyright:  东海运营
 */
@Configuration 
@Component
@Slf4j
@EnableScheduling
public class TQsjkrwTimer {
	
	@Value("${bdpath}")
	private String bdpath;
	
	@Value("${ftp.path}")
	private String ftppath;

	@Autowired
	private ITQsjkDao qsjkDao;
	
	private static String gang = "/";
	
	@Autowired
	private ITQsjkService qsjkService;
	
	private String nowdate;
	
	private boolean wjjcsh = false;//文件夹初始化
	
	@Autowired
	private ICommonDao commonDao;
	
	/**
	 * @Title: quzrtzTQsjkrw   
	 * @Description: 清算监控定时任务
	 * @author: clong
	 * @date:   2024年3月12日 下午1:58:02
	 */
	@Scheduled(fixedDelay = 300000)//休眠5分钟
    public void quzrtzTQsjkrw(){  
		try {
			String sfjyr = commonDao.queryTjyr();
			if("1".equals(sfjyr))
			{
				if(CommonUtil.isNotEmptyObj(nowdate))
				{
					if(!nowdate.equals(CommonUtil.getDate()))
					{
						nowdate = CommonUtil.getDate();
						qsjkDao.updateTQsjkrwLast();
					}
				}else{
					nowdate = CommonUtil.getDate();
				}
				
				String filepath = ftppath + gang + CommonUtil.getDateSystem() + FtpFilePthContant.FTP_JKZX_QSJK;
				
				TQsjkrwVo t = new TQsjkrwVo();
				List<TQsjkrwVo> list = qsjkDao.queryTQsjkrw(t);
				if(CommonUtil.isNotEmptyList(list))
				{
					String minTime = "";
					String maxTime = "";
					for(int i = 0;i < list.size();i ++)
					{
						String start = "";
						String end = "";
						if(list.get(i).getZxjg() == 0)
						{
							start = list.get(i).getZxsj().split(" - ")[0];
							end = list.get(i).getZxsj().split(" - ")[0];
						}else{
							start = list.get(i).getZxsj().split(" - ")[0];
							end = list.get(i).getZxsj().split(" - ")[1];
						}
						if(CommonUtil.isNotEmptyObj(minTime) && CommonUtil.isNotEmptyObj(maxTime))
						{
							if(QsjkFilesUtil.compareTime(minTime, start))
							{
								minTime = start;
							}
							if(!QsjkFilesUtil.compareTime(maxTime, end))
							{
								maxTime = end;
							}
						}else{
							minTime = start;
							maxTime = end;
						}
						if(!wjjcsh)
						{
							FTPUtil.mkdirPath(filepath + list.get(i).getRwbh());
						}
					}
					wjjcsh = true;
					
					if(QsjkFilesUtil.compareTime(QsjkFilesUtil.getSystemSfm(), minTime) && QsjkFilesUtil.compareTime(maxTime, QsjkFilesUtil.getSystemSfm()))
					{
						TQsjkrwLsVo l = new TQsjkrwLsVo();
						l.setZxsj(CommonUtil.getDate());
						List<TQsjkrwLsVo> lslist = qsjkDao.queryTQsjkrwLs(l);
						for(int i = 0;i < list.size();i ++)
						{
							if(list.get(i).getZxjg() == 0)
							{
								if(!"1".equals(list.get(i).getLast_zxjg()))
								{
									qsjkService.qsjkZx(list.get(i));
								}
							}else{
								List<TQsjkrwLsVo> newLslist = new ArrayList<>();
								int count = QsjkFilesUtil.getTimeTd(list.get(i).getZxsj().split(" - ")[0], list.get(i).getZxsj().split(" - ")[1], list.get(i).getZxjg());
								String zxsj = "";
								if(CommonUtil.isNotEmptyList(lslist))
								{
									int num = 0;
									for(int j = 0;j < lslist.size();j ++)
									{
										if(list.get(i).getId() == lslist.get(j).getPid())
										{
											if(num == 0)
											{
												zxsj = lslist.get(j).getZxsj().split(" ")[1];
												num ++;
											}
											newLslist.add(lslist.get(j));
										}
									}
									if(num == 0)
									{
										zxsj = list.get(i).getZxsj().split(" - ")[0];
									}
								}else{
									zxsj = list.get(i).getZxsj().split(" - ")[0];
								}
								if(newLslist.size() < count && QsjkFilesUtil.compareTime(QsjkFilesUtil.getSystemSfm(), QsjkFilesUtil.getTimeAddTd(zxsj, list.get(i).getZxjg() * newLslist.size())))
								{
									qsjkService.qsjkZx(list.get(i));
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("清算监控任务定时执行失败",e);
		}
	}
}
